/**
 * @author xuyh0817
 * @email xuyh0817@foxmail.com
 * @date 2022-03-15 01:22
 * @version 2.0.0
 * @doc todo
 * @website todo
 * @remark  如果要分发源码，需在本文件顶部保留此文件头信息！！
 */

/**
 * 随机生成字符串
 * @param length 字符串的长度
 * @return string 生成的字符串
 */
export const randomString = function(length = 32) {
  const chats = '0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM'
  let str = ''
  const randomNumber = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min)
  }
  for (let i = 0; i < length; i++) {
    const num = randomNumber(0, chats.length - 1)
    str += chats[num]
  }
  return str
}

/**
 * 获取指定id的项
 *
 * @param formOptions {Array} 表单项数组
 * @param id {String} 唯一id
 */
export const getFormOption = function(formOptions, id) {
  const findIndex = formOptions.findIndex(o => o.id === id)
  if (findIndex !== -1) {
    return formOptions[findIndex]
  }
  // 查找子项
  const filterOptions = formOptions.filter(o => o.isLayout)
  for (const option of filterOptions) {
    const attrs = option.attrs
    if (!attrs) {
      continue
    }
    const children = attrs.children
    if (!children) {
      continue
    }
    for (const child of children) {
      const formOption = getFormOption(child.children, id)
      if (formOption) {
        return formOption
      }
    }
  }
  return null
}

/**
 * 删除指定id的项
 *
 * @param formOptions {Array} 表单项数组
 * @param id {String} 唯一id
 */
export const removeFormOption = function(formOptions, id) {
  const findIndex = formOptions.findIndex(o => o.id === id)
  if (findIndex !== -1) {
    formOptions.splice(findIndex, 1)
    return true
  }
  // 查找子项
  const filterOptions = formOptions.filter(o => o.isLayout)
  for (const option of filterOptions) {
    const attrs = option.attrs
    if (!attrs) {
      continue
    }
    const children = attrs.children
    if (!children) {
      continue
    }
    for (const child of children) {
      const flag = removeFormOption(child.children, id)
      if (flag) {
        return true
      }
    }
  }
  return false
}
/**
 * 扁平项
 *
 * @param formOptions {Array} 表单项数组
 * @param flatMap {Object} 新增项
 */
export const flatFormOptions = function(formOptions = [], flatMap = {}) {
  formOptions.map((item) => {
    if (!item.id) {
      item.id = randomString()
    }
    flatMap[item.id] = item
  })
  const filterOptions = formOptions.filter(o => o.isLayout)
  for (const option of filterOptions) {
    const attrs = option.attrs
    if (!attrs) {
      continue
    }
    const children = attrs.children
    if (!children) {
      continue
    }
    for (const child of children) {
      flatFormOptions(child.children, flatMap)
    }
  }
}
/**
 * 复制指定id的项
 *
 * @param formOptions {Array} 表单项数组
 * @param id {String} 复制项的唯一id
 * @param copyOption {Object} 新增项
 */
export const copyFormOption = function(formOptions, id, copyOption = {}) {
  const findIndex = formOptions.findIndex(o => o.id === id)
  if (findIndex !== -1) {
    formOptions.splice(findIndex + 1, 0, copyOption)
    return true
  }
  // 查找子项
  const filterOptions = formOptions.filter(o => o.isLayout)
  for (const option of filterOptions) {
    const attrs = option.attrs
    if (!attrs) {
      continue
    }
    const children = attrs.children
    if (!children) {
      continue
    }
    for (const child of children) {
      const flag = copyFormOption(child.children, id, copyOption)
      if (flag) {
        return true
      }
    }
  }
  return false
}

/**
 * 扁平项
 *
 * @param tableColumnOptions {Array} 列表项数组
 * @param flatMap {Object} 扁平项
 */
export const flatTableColumnOptions = function(tableColumnOptions = [], flatMap = {}) {
  for (const option of tableColumnOptions) {
    if (!option.id) {
      option.id = randomString()
    }
    flatMap[option.id] = option
    if (option.isGroup && option.children) {
      flatTableColumnOptions(option.children, flatMap)
    }
  }
}

/**
 * 获取
 *
 * @param tableColumnOptions {Array} 列表项数组
 * @param id {String} id
 */

export const getTableColumnOption = function(tableColumnOptions = [], id = '') {
  for (const option of tableColumnOptions) {
    if (option.id === id) {
      return option
    }
    if (option.isGroup && option.children) {
      const tmp = getTableColumnOption(option.children, id)
      if (tmp) {
        return tmp
      }
    }
  }
  return undefined
}
